package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import model.AdminControlMessage;
import model.LoginDetails;
import model.UserSession;

public class LoginPanel extends JPanel implements ActionListener {

	private ClientFrame frame;
	private JLabel mailLabel;
	private JLabel passwordLabel;
	private JTextField mailTextField;
	private JPasswordField passwordTextField;
	private JButton loginButton;
	private JButton registerButton;
	private JButton liveFeedButton;

	public LoginPanel(ClientFrame f) {
		frame = f;
		this.setBounds(0, 0, frame.getWidth(), frame.getHeight());
		this.setBackground(Color.WHITE);
		this.setLayout(null);

		mailLabel = new JLabel("Guc mail: ");
		mailLabel.setBounds(300, 150, 100, 20);
		this.add(mailLabel);

		passwordLabel = new JLabel("Password: ");
		passwordLabel.setBounds(300, 200, 100, 20);
		this.add(passwordLabel);

		mailTextField = new JTextField();
		mailTextField.setBounds(400, 150, 120, 20);
		this.add(mailTextField);

		passwordTextField = new JPasswordField();
		passwordTextField.setBounds(400, 200, 120, 20);
		this.add(passwordTextField);

		registerButton = new JButton("Register");
		registerButton.setBounds(300, 250, 90, 20);
		registerButton.addActionListener(this);
		this.add(registerButton);

		loginButton = new JButton("Login");
		loginButton.setBounds(430, 250, 90, 20);
		loginButton.addActionListener(this);
		this.add(loginButton);

		liveFeedButton = new JButton("View Live Feed");
		liveFeedButton.setBounds(300, 300, 220, 20);
		liveFeedButton.addActionListener(this);
		this.add(liveFeedButton);

		this.repaint();

		frame.setContentPane(this);

	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == registerButton) {
			frame.goToRegisterPanel();
		}
		if (e.getSource() == liveFeedButton) {
			frame.goToLiveFeedPanel();
		}
		if (e.getSource() == loginButton) {

			if (mailTextField.getText().equals("")
					|| ClientFrame.getPasswordString(
							passwordTextField.getPassword()).equals("")) {
				JOptionPane.showMessageDialog(this,
						"Please enter your email and password !!");
			} else {
				try {
					Socket connectionSocket = new Socket("127.0.0.1", 4444);
					ObjectOutputStream objectOut = new ObjectOutputStream(
							connectionSocket.getOutputStream());
					ObjectInputStream objectIn = new ObjectInputStream(
							connectionSocket.getInputStream());

					LoginDetails loginDetails = new LoginDetails();
					loginDetails.setEmail(mailTextField.getText());
					loginDetails
							.setPassword(ClientFrame
									.getPasswordString(passwordTextField
											.getPassword()));
					objectOut.writeObject(loginDetails);
					objectOut.flush();

					String message = (String) objectIn.readObject();
					if (message.equals("invalid user")) {
						JOptionPane
								.showMessageDialog(this,
										"Sorry incorrect email or password. Please try again.");
						return;
					} else if (message.equals("account not activated")) {
						this.frame.goToActivatePanel(loginDetails);
						return;
					}

					else if (message.equals("admin user")) {
						objectOut.writeObject("get user session");
						objectOut.flush();
						UserSession userSession = (UserSession) objectIn
								.readObject();
						this.frame.setUserSession(userSession);

						AdminControlMessage adminControlMessage = new AdminControlMessage();
						adminControlMessage.setMessage("get voting status");
						adminControlMessage.setUserSession(frame
								.getUserSession());
						objectOut.writeObject(adminControlMessage);

						objectOut.flush();
						message = (String) objectIn.readObject();

						if (message.equals("Voting has closed")) {
							this.frame.goToViewResultsPanel();
						} else {
							this.frame.goToAdminPanel();
						}
						return;
					} else if (message.equals("valid user")) {
						objectOut.writeObject("get user session");
						objectOut.flush();
						UserSession userSession = (UserSession) objectIn
								.readObject();
						this.frame.setUserSession(userSession);

						objectOut.writeObject("get voting status");
						objectOut.flush();
						message = (String) objectIn.readObject();
						connectionSocket.close();
						if (message
								.startsWith("Sorry, voting hasn't started yet.")) {
							this.frame.goToDiplayMessagePanel(message);
						} else if (message.startsWith("You have voted for")) {
							this.frame.goToDiplayMessagePanel(message);
						}
						if (message.equals("Sorry, voting has closed.")) {
							this.frame.goToViewResultsPanel();
						} else if (message.equals("user hasn't voted")) {
							this.frame.goToVotePanel();
						}
					}
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
	}
}
